package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysClass;
import com.ruoyi.system.domain.SysTeacher;
import com.ruoyi.system.domain.SysTeacherClass;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysClassMapper;
import com.ruoyi.system.mapper.SysTeacherClassMapper;
import com.ruoyi.system.mapper.SysTeacherMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysTeacherService;

/**
 * 教师管理 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class SysTeacherServiceImpl implements ISysTeacherService
{
    @Autowired
    private SysClassMapper sysClassMapper;
    @Autowired
    private SysTeacherMapper teacherMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysTeacherClassMapper teacherClassMapper;

    /**
     * 查询教师列表
     * 
     * @param teacher 教师信息
     * @return 教师集合
     */
    @Override
    public List<SysTeacher> list(SysTeacher teacher)
    {
        return teacherMapper.list(teacher);
    }

    /**
     * 通过教师ID查询教师
     * 
     * @param teacherId 教师ID
     * @return 教师对象信息
     */
    @Override
    public SysTeacher selectById(Long teacherId)
    {
        return teacherMapper.selectById(teacherId);
    }

    /**
     * 新增教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    public int insert(SysTeacher entity)
    {
        teacherMapper.insert(entity);
        
        // 往用户表也添加一条数据，关联教师ID
        SysUser sysUser = new SysUser();
        sysUser.setAvatar(entity.getAvatar());
        sysUser.setUserName(entity.getUsername());
        sysUser.setRealName(entity.getTeacherName());
        sysUser.setNickName("--");
        sysUser.setPhonenumber("--");
        sysUser.setPassword(entity.getPassword());
        sysUser.setStatus(entity.getStatus());
        sysUser.setStudentId(0L);
        sysUser.setTeacherId(entity.getTeacherId());
        sysUser.setDelFlag("1");
        sysUserMapper.insertUser(sysUser);
        
        // 添加用户角色
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(sysUser.getUserId());
        sysUserRole.setRoleId(2L); // 教师固定角色ID为2
        sysUserRoleMapper.insert(sysUserRole);
        return 1;
    }

    /**
     * 修改教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    public int update(SysTeacher teacher)
    {
        int i = teacherMapper.update(teacher);
        
        SysUser sysUser = sysUserMapper.selectUserByTeacherId(teacher.getTeacherId());
        sysUser.setUserName(teacher.getUsername());
        if (teacher.getPassword() != null && teacher.getPassword().length() > 0) {
            String password = SecurityUtils.encryptPassword(teacher.getPassword());
            sysUser.setPassword(password);
		}
        sysUserMapper.updateUser(sysUser);
        return i;
    }

    /**
     * 删除教师信息
     * 
     * @param teacherId 教师ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteById(Long teacherId)
    {
        // 删除教师与班级关联
        teacherClassMapper.deleteByTeacherId(teacherId);
        teacherMapper.deleteById(teacherId);
        
        SysUser sysUser = sysUserMapper.selectUserByTeacherId(teacherId);
        sysUserMapper.deleteUserById(sysUser.getUserId());
        return 1;
    }

    /**
     * 批量删除教师信息
     * 
     * @param teacherIds 需要删除的教师ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteByIds(Long[] teacherIds)
    {
    	int i = 0;
        for (Long teacherId : teacherIds) {
            teacherClassMapper.deleteByTeacherId(teacherId);
            i++;
        }
        return i;
    }

    /**
     * 查询班级列表
     * 
     * @param sysClass 班级信息
     * @return 班级集合
     */
    @Override
    public List<SysClass> listClassUnBind(Long teacherId)
    {
        return sysClassMapper.selectUnBind(teacherId);
    }

    /**
     * 绑定班级
     * 
     * @param teacherId 教师ID
     * @param classIds 班级ID数组
     */
    @Override
    @Transactional
    public void insert(Long teacherId, Long[] classIds)
    {
    	teacherClassMapper.deleteByTeacherId(teacherId);
        if (classIds != null)
        {
            List<SysTeacherClass> list = new ArrayList<SysTeacherClass>();
            for (Long classId : classIds) {
            	
            	/*
            	 * classId传0，防止接口404
            	 */
            	if (classId == 0L) {
					continue;
				}
            	
            	Integer num = teacherClassMapper.countByClassId(classId);
            	if(num == 0) {
                    SysTeacherClass tc = new SysTeacherClass();
                    tc.setTeacherId(teacherId);
                    tc.setClassId(classId);
                    list.add(tc);
            	}
            }
            if (list.size() > 0)
            {
                teacherClassMapper.batchInsert(list);
            }
        }
    }
}